# coding:utf-8
from aiobsonrpc import (CreateServer, service_class, request, rpc_request, notification, rpc_notification)

HOST_PORT = ('127.0.0.1', 8000)


@service_class
class EchoServer(object):
    @request
    async def swapper(self, txt):
        return f"revers:{''.join(reversed(list(txt)))}"

    @request
    async def echo_times(self, num, n):
        return num * n

    @rpc_request
    async def long_process(self, rpc, a, b, c):
        # ways to send notifications:
        await rpc.invoke_notification('report_progress', 'server:long_process2')

        client = rpc.get_peer_proxy()
        # Make a request to client
        await client.n.report_progress('server:long_process1')
        result = await client.swapper('server:long_process')
        print(f"Msg from client:{result}")
        return a * b * c

    @notification
    async def notify(self, txt):
        print(f"--->notify:be call:{txt}")

    @rpc_notification
    async def rpc_notify(self, rpc, txt):
        client = rpc.get_peer_proxy()
        result = await client.swapper("server:rpc_notify")
        print(f"client_result:{result}")


if __name__ == '__main__':
    # Quick-and-dirty TCP Server:
    server = CreateServer(*HOST_PORT, services=EchoServer())
    server.run()
